#include <iostream>

using std::cout;
using std::endl;

class Complex
{
public:
    Complex(double dreal, double dimage)
    : _dreal(dreal)
    , _dimage(dimage)
    {   cout << "Complex(double,double)" << endl;   }

    void print() const
    {
        cout << _dreal << " + " << _dimage << " i" << endl;
    }

    //运算符重载之成员函数(非静态)
    Complex operator+(const Complex & rhs)
    {
        return Complex(_dreal + rhs._dreal, _dimage + rhs._dimage);
    }

    friend Complex & operator+=(Complex & lhs,  const Complex & rhs);
private:
    double _dreal;
    double _dimage;
};

//运算符重载时，其参数必须要有一个自定义类类型或枚举类型
/* int operator+(int x, int y) */
/* { */
/*     return x - y; */
/* } */

Complex & operator+=(Complex & lhs,  const Complex & rhs)
{
    lhs._dreal += rhs._dreal;
    lhs._dimage += rhs._dimage;
    return lhs;
}

int main()
{
    int a = 1, b = 2;
    int c = a + b;//内置类型的运算符不能重载
    cout << "c:" << c << endl;

    a += b;//a = a + b

    Complex c1(1, 2), c2(3, 4);
    cout << "c1:";
    c1.print();
    cout << "c2:";
    c2.print();

    Complex c3 = c1 + c2;
    cout << "c3:";
    c3.print();

    c1 += c2;
    operator+=(c1, c2);

    return 0;
}

